﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    private Transform target = null;
    private Vector3 offset;
    private Vector2 velocity;

    // Update is called once per frame
    void Update()
    {
        if (target == null)
        {
            var go = GameObject.FindGameObjectWithTag("Player");
            if (null != go)
            {
                target = go.transform;
                offset = target.position - transform.position;
            }
            if (null == target)
            {
                return;
            }
        }

        float posY = Mathf.SmoothDamp(transform.position.y, target.position.y - offset.y, ref velocity.y, 0.05f);
        if (posY > transform.position.y)
        {
            float posX = Mathf.SmoothDamp(transform.position.x, target.position.x - offset.x, ref velocity.x, 0.05f);
            transform.position = new Vector3(posX, posY, transform.position.z);
        }
    }
}
